package com.ly.lw.styledo.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.BaseBrandAdapter;
import com.ly.lw.styledo.adapter.BrandAllAdapter;
import com.ly.lw.styledo.bean.Brand;
import com.ly.lw.styledo.bean.BrandLogo;
import com.ly.lw.styledo.bean.ResultBrand;
import com.ly.lw.styledo.config.UrlConstants;
import com.ly.lw.styledo.utils.FileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sks on 15-7-7.
 */
public class BrandBaseFragment extends Fragment {
    public static final String SHOP_GRP_CD = "shop_grp_cd";


    @ViewInject(R.id.brand_list)
    private ListView listView;
    private String shop_grp_cd;
    private BitmapUtils bitmapUtils;
    private BaseBrandAdapter baseBrandAdapter;

    private ArrayList<BrandLogo> listBrandLogo = new ArrayList<>();




    private void initBitmap() {
        bitmapUtils = new BitmapUtils(getActivity().getApplicationContext(), FileUtils.getImageCache());
        //bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_default_item);
        // bitmapUtils.configDefaultLoadingImage(R.color.white);

        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        bitmapUtils.configDefaultImageLoadAnimation(animation);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//����ͼƬѹ������

        bitmapUtils.configMemoryCacheEnabled(true);
        bitmapUtils.configDiskCacheEnabled(true);

    }

    public static BrandBaseFragment newInstance(String shop_grp_cd) {
        BrandBaseFragment fragment = new BrandBaseFragment();
        Bundle args = new Bundle();
        args.putString(SHOP_GRP_CD, shop_grp_cd);
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBitmap();
        if (getArguments() != null) {
            shop_grp_cd = getArguments().getString(SHOP_GRP_CD);

        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_brand_all_layout, container, false);
        ViewUtils.inject(this, view);
        initView();
        loadData();
        return view;
    }

    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams requestParams = getRequestParams();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConstants.ALL_BRAND_URL,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获取服务器返回的json数据
                String result = responseInfo.result;
                Brand brand = JSONObject.parseObject(result, Brand.class);
                if (brand != null) {
                    ResultBrand resultBrand = brand.getResult();

                    if (resultBrand != null) {
                        List<BrandLogo> brandLogo = resultBrand.getList();
                        listBrandLogo.addAll(brandLogo);
                        baseBrandAdapter.notifyDataSetChanged();

                    }


                }


            }

            @Override
            public void onFailure(HttpException e, String s) {


            }
        });
    }

    private void initView() {
        baseBrandAdapter = new BaseBrandAdapter(listBrandLogo, getActivity());
        listView.setAdapter(baseBrandAdapter);

    }


    /**
     * appVersion	1.4.6
     shop_grp_cd	30
     __pno	4010
     osVersion   	5.1.1
     os	android

     * @return
     */
    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("appVersion", "1.4.6");
        params.addBodyParameter("shop_grp_cd","10" );
        params.addBodyParameter("shop_no", "4010");
        params.addBodyParameter("os", "android");
       // params.addBodyParameter("osVersion", "");

        return params;
    }
}
